package com.ba.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionUtil {

	private Connection connection;
	private Statement statement;
	private String driverName;
	private String connectionString;
	private String username;
	private String password;

	public ConnectionUtil(String driverName, String connectionString,
			String username, String password) {
		this.driverName = driverName;
		this.connectionString = connectionString;
		this.username = username;
		this.password = password;
	}

	public Connection getConnection() {
		try {
			Class.forName(driverName);
			connection = DriverManager.getConnection(connectionString,
					username, password);
			return connection;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (connection == null)
			throw new RuntimeException("Connection is null");
		return connection;
	}

	public void closeConnection() {
		try {
			if (statement != null)
				statement.close();
			if (connection != null)
				connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Statement getStatement() {
		try {
			statement = getConnection().createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return statement;
	}
}
